package com.rykj.qxj.rest;

import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.rykj.qxj.server.common.exception.ApiManageIsNullExceptiuon;
import com.rykj.qxj.server.common.exception.ApiManageSaveExceptiuon;
import com.rykj.qxj.server.common.exception.ApiVersionManageIsNullExceptiuon;
import com.rykj.qxj.server.common.exception.ApiVersionManageaveExceptiuon;
import com.rykj.qxj.server.model.param.ApiManagerOutsideUrlParams;
import com.rykj.qxj.util.Result;
import com.rykj.qxj.util.net.HttpClientUtils;

import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/test-data-api")
public class TestController {
	@GetMapping("/getFile")
	public void saveApiManageOutsideUrl(HttpServletRequest request,HttpServletResponse response) throws IOException{
		Map<String, String[]> parameterMap = request.getParameterMap();
		  InputStream in = HttpClientUtils.sendFileHttpGet(null,"http://localhost/100.jpg","UTF-8");
		  OutputStream outputStream = new BufferedOutputStream(response.getOutputStream());
			//创建存放文件内容的数组
			byte[] buff =new byte[1024];
			//所读取的内容使用n来接收
			int n;
			//当没有读取完时,继续读取,循环
			while((n=in.read(buff))!=-1){
				//将字节数组的数据全部写入到输出流中
				outputStream.write(buff,0,n);
			}
			//强制将缓存区的数据进行输出
			System.out.println(outputStream);
			outputStream.flush();
			//关流
			outputStream.close();
			in.close();
		
	}
	@GetMapping("/apiDemo")
	public void apiDemo(HttpServletRequest request,HttpServletResponse response) throws IOException{
		Map<String, Object> requestParamMapByGet = getRequestParamMapByGet(request);
		System.out.println(requestParamMapByGet.toString());
		
	}
	
	private Map<String,Object> getRequestParamMapByGet(ServletRequest request) {
		Map<String, String[]> parameterMap = request.getParameterMap();
		Map<String, Object> pMap =new HashMap<String,Object>();
		Set<String> keys= parameterMap.keySet();
		for (String key : keys) {
			pMap.put(key, parameterMap.get(key)[0]);
			
		}
		return pMap;
	}
}
